<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row v-show="showSearch">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
      <el-form-item label="所属子站" prop="substationId" >
        <!-- <el-input placeholder="请输入所属子站" v-model="state.queryForm.substationId" /> -->
        <el-text>{{ state.queryForm.substationName }}</el-text>
        <el-button type="primary"   @click="selectRelateSubstation">选择</el-button>
        <el-button type="primary"   @click="clearRelateSubstation">清除</el-button>
      </el-form-item>
      <el-form-item label="所属设备" prop="equipmentId" >
        <!-- <el-input placeholder="请输入所属设备" v-model="state.queryForm.equipmentId" /> -->
        <el-text>{{ state.queryForm.equipmentName }}</el-text>
        <el-button type="primary"   @click="selectRelateEquipment">选择</el-button>
        <el-button type="primary"   @click="clearRelateEquipment">清除</el-button>
      </el-form-item>
      <el-form-item label="因子代码" prop="factorCode" >
        <el-input placeholder="请输入因子代码" v-model="state.queryForm.factorCode" />
      </el-form-item>
      <el-form-item label="报警时间" prop="alarmDate" >
        <el-date-picker
      v-model="state.queryForm.alarmDate"
      @change="selectAlarmDate"
      type="datetimerange"
      value-format="YYYY-MM-DD HH:mm:ss"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期">
    </el-date-picker>
      </el-form-item>
          <el-form-item>
            <el-button icon="search" type="primary" @click="getDataList">
              查询
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <div class="mb8" style="width: 100%">
          <!-- <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
            v-auth="'environment_alarm_add'">
            新 增
          </el-button> -->
          <el-button plain :disabled="multiple" icon="Delete" type="primary"
            v-auth="'environment_alarm_del'" @click="handleDelete(selectObjs)">
            删除
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'environment_alarm_export'"
                @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
            @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border 
        :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
        @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column type="index" label="#" width="40" />
          <el-table-column prop="substationName" label="所属子站"  show-overflow-tooltip/>
          <el-table-column prop="equipmentName" label="所属设备"  show-overflow-tooltip/>
          <el-table-column prop="factorCode" label="因子代码"  show-overflow-tooltip/>
          <el-table-column prop="alarmVal" label="数值"  show-overflow-tooltip/>
          <el-table-column prop="createTime" label="报警时间"  show-overflow-tooltip/>
          <el-table-column prop="status" label="状态" show-overflow-tooltip>
      <template #default="scope">
                <dict-tag :options="alarm_status" :value="scope.row.status"></dict-tag>
            </template>
          </el-table-column>
          <el-table-column prop="handlerResult" label="处理结果"  show-overflow-tooltip/>
          <el-table-column prop="handlerTime" label="处理时间"  show-overflow-tooltip/>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button icon="edit-pen" text type="primary" v-auth="'environment_alarm_edit'"
              @click="formDialogRef.openDialog(scope.row.id)">处理</el-button>
            <el-button icon="delete" text type="primary" v-auth="'environment_alarm_del'" @click="handleDelete([scope.row.id])">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" ></form-dialog>

     <!-- 子站选择  -->
    <el-dialog width="100%"
    class="substation-dialog" title="选择子站" v-model="substationVisible"
       draggable>
    <substation ref="substationRef" @getSubstationId="getSubstationId" isReleteSelect="true" ></substation>
    </el-dialog>

    <el-dialog width="100%"
    class="substation-dialog" title="选择设备" v-model="equipmentVisible"
       draggable>
    <equipment ref="equipmentRef" @getEquipmentId="getEquipmentId" isReleteSelect="true" ></equipment>
    </el-dialog>

  </div>
</template>

<script setup lang="ts" name="systemAlarm">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList, delObjs } from "/@/api/environment/alarm";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { useDict } from '/@/hooks/dict';
const substation = defineAsyncComponent(() => import('/@/views/environment/substation/index.vue'));
const equipment = defineAsyncComponent(() => import('/@/views/environment/equipment/index.vue'));

const substationVisible = ref(false)
const substationRef = ref()

const equipmentVisible = ref(false)
const equipmentRef = ref()
// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典

const { alarm_status } = useDict('alarm_status')
// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})
 

const selectAlarmDate =(val)=>{  
  console.log("date:",val);
}

//打开子站选择
const selectRelateSubstation = ()=>{
  substationVisible.value=true;
  
};
const getSubstationId =(row)=>{
  console.log('getSubstationId row:'+row);
  state.queryForm.substationId=row.id;
  state.queryForm.substationName=row.name;
  substationVisible.value=false;
}

const clearRelateSubstation=()=>{
  state.queryForm.substationId='';
  state.queryForm.substationName='';
}

//打开设备选择
const selectRelateEquipment = ()=>{
  equipmentVisible.value=true;
  
};
const getEquipmentId =(row)=>{
  console.log('getEquipmentId row:'+row);
  state.queryForm.equipmentId=row.id;
  state.queryForm.equipmentName=row.name;
  equipmentVisible.value=false;
}

const clearRelateEquipment=()=>{
  state.queryForm.equipmentId='';
  state.queryForm.equipmentName='';
}

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
	tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  state.queryForm.substationName='';
  state.queryForm.equipmentName='';
  state.queryForm.startTime='';
  state.queryForm.endTime='';
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/environment/alarm/export',Object.assign(state.queryForm, { ids: selectObjs }), 'alarm.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({ id }) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>
<style lang="less" scope>

.substation-dialog {
    // display: flex;
    // justify-content: center;
    // align-items: Center;
    overflow: hidden;
    height: 100%;
    .el-dialog {
        margin: 0 auto !important;
        position: relative;
       
        .el-dialog__header{
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            width: 100%;
            height: 60px;
            z-index: 1;
        }
        .el-dialog__body {
            width: 100%;
            overflow: hidden;
            overflow-y: auto;
            max-height: 90vh;//最大高度为视口高度的90%
            padding-top: 80px;
            padding-bottom: 100px;
            z-index: 1;            
        }
        .el-dialog__footer{
            position: absolute;
            left: 0;
            bottom: 0;
            right: 0;
            width: 100%;
            height: 80px;
            z-index: 1;
        }
    }
}


</style> 